package scjp.preparation.exam;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Formatter;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.regex.Pattern;

public class Format {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Formatter formatter = new Formatter();
		Calendar calendar = Calendar.getInstance(Locale.US);
		calendar.set(/* year = */2012, /* month = */Calendar.FEBRUARY,
		/* date = */1);
		formatter.format("%tY/%tB/%<td", calendar, calendar);
		System.out.println(formatter);
		Set<String> set = new CopyOnWriteArraySet<String>();
		Path currPath = Paths.get(".");

		Object o1 = 10;
		int i = 10;
		Integer anInteger = 10;
		String s = "Hello";
		Integer i1 = cast(o1); // ERROR2
		Integer i2 = cast(i); // ERROR3
		Integer i3 = cast(10); // ERROR4
		Integer i4 = cast(anInteger);
		System.out.printf("i1 = %d, i2 = %d, i3 = %d, i4 = %d %n", i1, i2, i3, i4);

		Path aFilePath = Paths.get("D:\\dir\\file.txt");
		Iterator<Path> paths = aFilePath.iterator();
		while (paths.hasNext()) {
			System.out.print(paths.next() + " ");
		}
		
		List list = Arrays.asList(20, 5, 10, 10);
		System.out.println(list);
		System.out.println(new HashSet(list));
		System.out.println(new TreeSet(list));
		System.out.println(new ConcurrentSkipListSet(list));
		
		String date = "10-01-2012"; // 10th January 2012 in dd-mm-yyyy format
		String [] dateParts = date.split("-");
		System.out.print("Using String.split method: ");
		for(String part : dateParts) {
		System.out.print(part + " ");
		}
		System.out.print("\nUsing regex pattern: ");
		Pattern datePattern = Pattern.compile("-");
		dateParts = datePattern.split(date);
		for(String part : dateParts) {
		System.out.print(part + " ");
		}
		System.out.println("\n");
		Class c1 = new ArrayList<String>().getClass(); // LINE A
		Class c2 = ArrayList.class; // LINE B
		System.out.println(c1 == c2);
	}

	@SuppressWarnings("unchecked")
	static <E> E cast(Object item) { // ERROR1
		return (E) item;

	}

}
